package com.ssh.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ssh.model.Content;
public class ContentDao extends HibernateDaoSupport implements IContentDao{
	/* (non-Javadoc)
	 * @see com.ssh.dao.IContentDao#save(com.ssh.model.Content)
	 */
	@Override
	public void save(Content c) {
		try {
			getHibernateTemplate().save(c);
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.ssh.dao.IContentDao#delete(com.ssh.model.Content)
	 */
	@Override
	public void delete(Content c) {
		try {
			getHibernateTemplate().delete(c);
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.ssh.dao.IContentDao#findById(java.lang.String)
	 */
	@Override
	public Content findById(java.lang.Integer ctid) {
		try {
			Content c = (Content) getHibernateTemplate().get(
					"com.ssh.model.Content", ctid);
			return c;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	public List findByProperty2(String propertyName, Object value) {
		try {
			String hql = "from Content as model where model."
					+ propertyName + "= " +"'"+value+"'";
			Query query = getSession().createQuery(hql);
			return query.list();
					} catch (RuntimeException re) {
			throw re;
		}
	}
	
	public List findByUserid(Object value){
		String propertyName="userid";
		return findByProperty2(propertyName, value);
	}
	
	/* (non-Javadoc)
	 * @see com.ssh.dao.IContentDao#findAll()
	 */
	@Override
	public List findAll() {
		try {
			String queryString = "from Content order by id desc";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.ssh.dao.IContentDao#attachDirty(com.ssh.model.Content)
	 */
	@Override
	public void attachDirty(Content c) {
		try {
			getHibernateTemplate().saveOrUpdate(c);
		} catch (RuntimeException re) {
			throw re;
		}
	}

}
